package com.schoolai.schoolset.config;


import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.lang.reflect.Method;
import java.time.Duration;

@Configuration
// 开启方法缓存必须手动开启
@EnableCaching
public class RedisCacheManagerConfig extends CachingConfigurerSupport {


    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Object> template=new RedisTemplate();
//        为String类型key设置序列化器
        template.setKeySerializer(new StringRedisSerializer());
//        为String类型value设置序列化器
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//        为Hash类型key设置序列化器
        template.setHashKeySerializer(new StringRedisSerializer());
//        为Hash类型value设置序列化器
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
    //    重写Cache序列化
    @Bean
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate){
        RedisCacheManager redisCacheManager =
                RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisTemplate.getConnectionFactory())
                        .cacheDefaults(getCacheConfigurationWithTtl(redisTemplate,60*60))
                        .withCacheConfiguration("image",getCacheConfigurationWithTtl(redisTemplate,60*60  ))
                        .withCacheConfiguration("rich_txt",getCacheConfigurationWithTtl(redisTemplate,60))
                        .withCacheConfiguration("news",getCacheConfigurationWithTtl(redisTemplate,60*60))
                        .withCacheConfiguration("userurl",getCacheConfigurationWithTtl(redisTemplate,60))
                        .transactionAware()
                        .build();
        redisCacheManager.getCacheConfigurations();
        return redisCacheManager;
    }
    RedisCacheConfiguration getCacheConfigurationWithTtl(RedisTemplate<String,Object> template, long seconds){
        return RedisCacheConfiguration
                .defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getStringSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getValueSerializer()))
                .entryTtl(Duration.ofSeconds(seconds));

    }
}
